package 棋盘游戏;

import java.util.*;

public class Main {
    static int N = 1 << 16;
    static int[] st = new int[N];
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        int start = input();
        int end = input();
        System.out.println(bfs(start, end));
    }

    private static int bfs(int start, int end) {
        if (start == end) {
            return 0;
        }
        Queue<Integer> q = new LinkedList<>();
        q.offer(start);
        Arrays.fill(st, -1);
        st[start] = 0;
        while (!q.isEmpty()) {
            int t = q.poll();
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    for (int k = 0; k < 4; k++) {
                        int a = i + dx[k];
                        int b = j + dy[k];
                        if (a < 0 || a >= 4 || b < 0 || b >= 4) {
                            continue;
                        }
                        int state = bit_swap(t, i * 4 + j, a * 4 + b);
                        if (st[state] != -1) {
                            continue;
                        }
                        st[state] = st[t] + 1;
                        if (state == end) {
                            return st[end];
                        }
                        q.offer(state);
                    }
                }
            }
        }
        return -1;
    }

    private static int bit_swap(int t, int x, int y) {
        int a = t >> x & 1;
        int b = t >> y & 1;
        t -= a << x;
        t -= b << y;
        t += a << y;
        t += b << x;
        return t;
    }

    private static int input() {
        int state = 0;
        for (int i = 0; i < 4; i++) {
            String s = in.next();
            for (int j = 0; j < 4; j++) {
                state += (s.charAt(j) - '0') * (1 << (i * 4 + j));
            }
        }
        return state;
    }
}
